class Cons {
    constructor(car, cdr = undefined) {
        this.car = car;
        this.cdr = cdr;
    }

    static fromArray(arr, start = 0) {
        if (start >= arr.length - 1) {
            return new Cons(arr[start]);
        } else {
            return new Cons(arr[start], Cons.fromArray(arr, start + 1));
        }
    }

    toArray(arr = []) {
        arr.push(this.car);

        if (this.cdr) {
            this.cdr.toArray(arr);
        }

        return arr;
    }

    map(f) {
        var cur = this;
        var i = 1;

        while (cur) {
            cur.car = f(cur.car, i, cur);
            cur = cur.cdr;
        }
    }

    filter(f) {
        var res = [];
        var cur 
    }

    get length() {
        var cur = this;
        var len = 0;

        while (cur && cur.car !== undefined) {
            len++;
            cur = cur.cdr;
        }
    }

    *[Symbol.iterator]() {
        var cur = this;

        while (cur) {
            yield cur.car;
            cur = cur.cdr;
        }
    }
}

export { Cons };